﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.Plastic.Newtonsoft.Json;
using UnityEditor;
using UnityEngine;

internal class LinkWindow : EditorWindow
{
    [MenuItem("Tools/Link/MergeLink")]
    private static void ShowWindow()
    {
        EditorWindow.GetWindow<LinkWindow>().Show();
    }

    [MenuItem("Tools/Link/MergeLinkList")]
    public static void Merge()
    {
        var path = Path.Combine(Application.dataPath, "linkMergeList.txt");
        if (!File.Exists(path))
        {
            Debug.Log($"File {path} could not be found");
            return;
        }

        var content = File.ReadAllText(path);
        List<string> paths = new List<string>();
        foreach (var item in content.Split('\n'))
        {
            var xmlPath = item.Trim();
            if (File.Exists(xmlPath))
            {
                paths.Add(xmlPath);
            }
        }

        if (paths.Count > 0)
        {
            Link.Merge(paths, Application.dataPath + "/link.xml");
        }
    }

    private Vector2 listPosition;
    private List<string> paths = new List<string>();

    private void OnEnable()
    {
        var json = EditorPrefs.GetString("linkMerge");
        if (!string.IsNullOrEmpty(json))
        {
            paths = JsonConvert.DeserializeObject<List<string>>(json);
        }

        if (paths.Count == 0)
        {
            paths.Add(string.Empty);
        }
    }

    private void OnDestroy()
    {
        var json = JsonConvert.SerializeObject(paths);
        EditorPrefs.SetString("linkMerge", json);
    }

    private void OnGUI()
    {
        if (GUILayout.Button("合并"))
        {
            Link.Merge(paths, Application.dataPath + "/link.xml");
        }
        listPosition = EditorGUILayout.BeginScrollView(listPosition);
        {
            for (int i = 0; i < paths.Count; i++)
            {
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField(paths[i]);
                    if (GUILayout.Button("..."))
                    {
                        OnClick(i);
                    }
                    if (!string.IsNullOrEmpty(paths[i]) && GUILayout.Button("X"))
                    {
                        paths.RemoveAt(i);
                        Event.current.Use();
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
        EditorGUILayout.EndScrollView();
    }

    private void OnClick(int index)
    {
        var result = EditorUtility.OpenFolderPanel("select project", "", "");
        if (string.IsNullOrEmpty(result))
            return;

        addToList(Path.Combine(result, "link.xml"));
        addToList(Path.Combine(result, "Assets/link.xml"));

        bool addToList(string path)
        {
            if (File.Exists(path))
            {
                if (!paths.Contains(path))
                {
                    paths[index] = path;
                    if (index + 1 == paths.Count)
                    {
                        paths.Add(string.Empty);
                    }
                    return true;
                }
            }
            return false;
        }
    }
}